package com.qiche.controller.mgr;

import com.github.pagehelper.PageInfo;
import com.qiche.aspect.Result;
import com.qiche.constant.AccountType;
import com.qiche.model.Merchant;
import com.qiche.model.Region;
import com.qiche.model.RegionWelfare;
import com.qiche.model.User;
import com.qiche.service.MerchantService;
import com.qiche.service.RegionService;
import com.qiche.service.RegionWelfareService;
import com.qiche.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 地区福利管理控制器
 * Created by Linzw on 2018/01/24.
 */
@Api(value = "/welfares",description = "区域管理控制器")
@RestController
@RequestMapping("/welfares")
public class RegionWelfareController {

	@Resource
	private RegionWelfareService welfareService;

	@Resource
	private RegionService regionService;

	@Resource
	private MerchantService merchantService;

	@ApiOperation(value ="根据条件和分页获取地区福利列表" )
	@RequestMapping
	public Map<String, Object> getAll(RegionWelfare welfare,
			@RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int limit) {
		Map<String, Object> map = new HashMap<>();
		User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
		Merchant merchant = merchantService.selectById(currentUser.getMerchantId().longValue());
		// 如果是管理员身份,则不添加筛选条件
		if (AccountType.ADMIN!=currentUser.getAccountType()) {
			welfare.setRegionId(merchant.getRegionId());
		}
		PageInfo<RegionWelfare> pageInfo = welfareService.selectByPage(welfare, page, limit);
		map.put("code", 0);
		map.put("msg", "查询成功");
		map.put("count", pageInfo.getTotal());
		map.put("data", pageInfo.getList());
		return map;
	}

	@ApiOperation(value ="添加地区福利" )
	@PostMapping
	public Result add(@RequestBody RegionWelfare welfare) {
		Region region=regionService.selectById(welfare.getRegionId());
		if(region!=null){
			welfare.setRegionName(region.getRegionName());
		}
		int insertCount = welfareService.save(welfare);
		if (insertCount > 0) {
		return Result.createBySuccessMessage("保存成功");
		}
		return Result.createByErrorMessage("保存失败");
	}

	@ApiOperation(value ="更新地区福利" )
	@PutMapping
	public Result update(@RequestBody RegionWelfare welfare) {
		Region region=regionService.selectById(welfare.getRegionId());
		if(region!=null){
			welfare.setRegionName(region.getRegionName());
		}
		int updateCount = welfareService.updateNotNull(welfare);
		if (updateCount > 0) {
			return Result.createBySuccessMessage("更新成功");
		}
		return Result.createByErrorMessage("更新失败");
	}

	@ApiOperation(value ="根据ID获取地区福利信息" )
	@GetMapping(value = "/getById/{id}")
	public Result getById(@PathVariable("id") Long welfareId) {
		RegionWelfare welfare = welfareService.selectById(welfareId);
		return Result.createBySuccess(welfare);
	}

	@ApiOperation(value ="根据当前地登录门店所属的地区获取福利列表" )
	@GetMapping(value = "/getByRegion")
	public Result getByRegion() {
		User currentUser = UserUtils.getCurrentUser();
		Merchant merchant = merchantService.selectById(currentUser.getMerchantId().longValue());
		List<RegionWelfare> welfares = welfareService.selectByRegionId(merchant.getRegionId());
		return Result.createBySuccess(welfares);
	}

	@ApiOperation(value ="根据ID删除地区福利信息" )
	@DeleteMapping(value = "/delete/{id}")
	public Result delete(@PathVariable("id") Long id) {
		return 	welfareService.delRegionWelfare(id);
	}

}
